public class work3 {
    //给定一个有序整型数组, 实现二分查找
    public static int find(int[] array,int num) {
        int i = 0, j = array.length - 1;
        int mid = (i + j) / 2;
        while (true){
            if (array[mid] < num) {
                i = mid;
                mid = (i + j) / 2;
            }
        if (array[mid] > num) {
            j = mid;
            mid = (i + j) / 2;
        } else if (array[mid] == num) {
            return mid;
        }if (array[i] > num || array[j] < num){
                return -1;
            }
    }
    }

    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6};
         int num = find(array,7);
        System.out.println(num);
    }
}
